package com.hencoderplus.demo1;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v7.widget.AppCompatEditText;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;

/**
 * hencoderplus
 * Created by lzw on 2018/11/7. 15:07:22
 * 邮箱：632393724@qq.com
 * All Rights Saved! Chongqing AnYun Tech co. LTD
 */
public class MaterialEditText extends AppCompatEditText {


    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private static final int TEXT_MARGIN = (int) Utils.dp2Px(10);
    private static final int TEXT_SIZE = (int) Utils.dp2Px(12);
    private static final int TEXT_VERTICAL_OFFSET = (int) Utils.dp2Px(12);
    private static final int TEXT_HORZIONTAL_OFFSET = (int) Utils.dp2Px(5);
    private static final int TEXT_ANIM_OFFSET = (int) Utils.dp2Px(5);
    private boolean floatingLabelShown = false;
    private float floatingLabeFraction = 0;
    private ObjectAnimator objectAnimator;
    private boolean setUseFloatingLabel = true;
    private Rect backgroundPadding = new Rect();

    {



    }

    private void onUseFloatingLabelChange(){
        getBackground().getPadding(backgroundPadding);
        if (setUseFloatingLabel) {

            setPadding(getPaddingLeft(), TEXT_MARGIN + TEXT_SIZE + backgroundPadding.top, getPaddingRight(), getPaddingBottom());


        }else{
            setPadding(getPaddingLeft(), backgroundPadding.top, getPaddingRight(), getPaddingBottom());
        }

    }
    public MaterialEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);

    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.MaterialEditText);
        setUseFloatingLabel = typedArray.getBoolean(R.styleable.MaterialEditText_useFloatingLabel,true);

        onUseFloatingLabelChange();
        paint.setTextSize(TEXT_SIZE);
        addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (setUseFloatingLabel){
                    if (floatingLabelShown && TextUtils.isEmpty(s)) {
                        floatingLabelShown = false;
                        getAnimator().reverse();
                    } else if (!floatingLabelShown && !TextUtils.isEmpty(s)) {
                        floatingLabelShown = true;
                        getAnimator().start();
                    }
                }

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    private float getFloatingLabeFraction() {
        return floatingLabeFraction;
    }

    public void setUseFloatingLabel(boolean floatingLabel) {
        if (setUseFloatingLabel != floatingLabel) {

            this.setUseFloatingLabel = floatingLabel;
            onUseFloatingLabelChange();
        }

    }

    private void setFloatingLabeFraction(float floatingLabeFraction) {
        this.floatingLabeFraction = floatingLabeFraction;
        invalidate();
    }

    private ObjectAnimator getAnimator() {
        if (objectAnimator == null) {
            objectAnimator = ObjectAnimator.ofFloat(MaterialEditText.this,
                    "floatingLabeFraction", 0, 1);
        }
        return objectAnimator;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setAlpha((int) (0xff * floatingLabeFraction));
        float extraOffset = TEXT_ANIM_OFFSET * (1 - floatingLabeFraction);
        canvas.drawText(getHint().toString(), TEXT_HORZIONTAL_OFFSET, TEXT_VERTICAL_OFFSET + extraOffset, paint);

    }
}
